package com.sitech.openapi.service;

import com.sitech.openapi.domain.Order.CollectionOrderNotification;
import com.sitech.openapi.domain.Order.ProcessingStatus;
import com.sitech.openapi.mapper.YiDiXiaoHuServiceMapper;
import com.sitech.openapi.utils.DateUtil;
import com.sitech.openapi.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class YiDiXiaoHuServiceImpl implements YiDiXiaoHuService{
    //@Autowired
    //DifferCancelOrderInfoMapper differCancelOrderInfoMapper;

    @Autowired
    YiDiXiaoHuServiceMapper yiDiXiaoHuServiceMapper;

    @Override
    public String dealCollectOrder(CollectionOrderNotification collectionOrderNotification) {
        //系统时间
        Date date = DateUtil.getSysDate();
        //省份工单流水
        String orderNbr = collectionOrderNotification.getOrderNbr();
        if(StringUtil.isSpaceOrNull(orderNbr)){
            throw new RuntimeException("省份工单流水orderNbr不能为空！");
        }
        //通知类型(1000：工单信息发布，1100：工单状态变更)
        String notifyType = collectionOrderNotification.getNotifyType();
        if(StringUtil.isSpaceOrNull(notifyType)){
            throw new RuntimeException("工单通知类型notifyType不能为空！");
        }
        //工单状态
        String statusCd = collectionOrderNotification.getStatusCd();
        //查询省内是否存在工单
        /*DifferCancelOrderInfoExample differCancelOrderInfoExample = new DifferCancelOrderInfoExample();
        differCancelOrderInfoExample.createCriteria().andOrderNbrEqualTo(orderNbr);
        List<DifferCancelOrderInfo> differCancelOrderInfos = differCancelOrderInfoMapper.selectByExample(differCancelOrderInfoExample);
        if (differCancelOrderInfos == null || differCancelOrderInfos.size() == 0) {
            throw new RuntimeException("省内不存在工单" + orderNbr + "的信息！");
        }*/

        //工单状态变更
        if(notifyType.equals("1100")){
            if(StringUtil.isSpaceOrNull(statusCd)){
                throw new RuntimeException("工单状态statusCd不能为空！");
            }
            Map<String,String> paramMap = new HashMap<String,String>();
            paramMap.put("PARA_TYPE_ID","642");
            paramMap.put("UP_PARA_ID","2");
            paramMap.put("PARA_ID",statusCd);
            List<Map<String,String>> statusCdList = (List<Map<String,String>>)yiDiXiaoHuServiceMapper.selectCodeName(paramMap);
            if(statusCdList != null && statusCdList.size() > 0){
                //工单备注
                String remark = collectionOrderNotification.getRemark() + ";";
                String reasonDesc = collectionOrderNotification.getReasonDesc() + ";";
                List<ProcessingStatus> processingStatuss = collectionOrderNotification.getProcessingStatuss();
                if(processingStatuss != null && processingStatuss.size() > 0){
                    ProcessingStatus processingStatus = processingStatuss.get(0);
                    remark += processingStatus.getRemark() + ";";
                    reasonDesc += processingStatus.getReasonDesc() + ";";
                }
                if(remark.startsWith(";") && remark.length() > 1){
                    remark = remark.substring(1,remark.length());
                }
                if(remark.endsWith(";")){
                    remark = remark.substring(0,remark.length() - 1);
                }
                if(reasonDesc.startsWith(";") && reasonDesc.length() > 1){
                    reasonDesc = reasonDesc.substring(1,reasonDesc.length());
                }
                if(reasonDesc.endsWith(";")){
                    reasonDesc = reasonDesc.substring(0,reasonDesc.length() - 1);
                }
                String note = "";
                if(!StringUtil.isSpaceOrNull(remark) || !StringUtil.isSpaceOrNull(reasonDesc)){
                    note = remark + ";" + reasonDesc;
                }
                //更新前
                Map<String,String> orderInfoHisMap = new HashMap<String,String>();
                orderInfoHisMap.put("orderNbr",orderNbr);
                orderInfoHisMap.put("hisLogin","aadmin");
                orderInfoHisMap.put("hisTime", DateUtil.toStringYmdHms(date));
                orderInfoHisMap.put("hisType","U");
                yiDiXiaoHuServiceMapper.insertDCOrderInfoToHis(orderInfoHisMap);
                //更新
                Map<String,String> orderInfoUpdateMap = new HashMap<String,String>();
                orderInfoUpdateMap.put("orderNbr",orderNbr);
                orderInfoUpdateMap.put("statusCd",statusCd);
                orderInfoUpdateMap.put("note",note);
                yiDiXiaoHuServiceMapper.updateDifferCancelOrderInfo(orderInfoUpdateMap);
                //更新后
                orderInfoHisMap.put("hisType","X");
                yiDiXiaoHuServiceMapper.insertDCOrderInfoToHis(orderInfoHisMap);
            }else{
                throw new RuntimeException("省内不存在statusCd=" + statusCd + "的工单状态！");
            }
        }

        return "";
    }
}
